<?php
class ImageObject
{
    /**
     * @name CreateImageCopyProportionToWidth
     * @param String  $imagefile
     * @param int $newWidth
     *
     * Description: resize the image proportion to the set width
     * File will be written as describe in the resizeImage function
     */
	public static function CreateImageCopyProportionToWidth($imagefile, $newWidth)
	{
		// get image width and height
		list($imgWidth, $imgHeight) = getimagesize($imagefile);

		// set the new height
		$newHeight = ($newWidth/$imgWidth) * $imgHeight;

		// generate the file
		self::resizeImage($imagefile, $newWidth, $newHeight);
	}

    /**
     * @name CreateImageCopyProportionToHeight
     * @param String $imagefile
     * @param int $newHeight
     *
     * Description: resize the image proportion to the set width
     * File will be written as describe in the resizeImage function
     */
	public static function CreateImageCopyProportionToHeight($imagefile, $newHeight)
	{
		// get the image width and height
		list($imgWidth, $imgHeight) = getimagesize($imagefile);

		// set the new width
		$newWidth = ($newHeight/$imgHeight) * $imgWidth;

		// generate the file
		self::resizeImage($imagefile, $newWidth, $newHeight);
	}

    /**
     * @name resizeImage
     * @param String $srcImg
     * @param int $newWidth
     * @param int $newHeight
     *
     * Description: write the new resized image in the thumbnail directory
     * of the source file having the same filename
     */
	protected function resizeImage($srcImg, $newWidth, $newHeight)
	{
        // set the thumbnail directory
        $thumbnail = ".thumbnail";

        // get the source image property: width and height
		list($imgWidth, $imgHeight) = getimagesize($srcImg);

		// create tempfile
		$newImg = imagecreatetruecolor($newWidth, $newHeight);
		$_srcImg = imagecreatefromjpeg($srcImg);

		// resize
		imagecopyresampled($newImg, $_srcImg, 0,0,0,0,$newWidth, $newHeight, $imgWidth, $imgHeight);

		//check if folder is writable
		if(!is_dir(dirname($imagefile)."/".$thumbnail))
			mkdir(dirname($imagefile)."/".$thumbnail);

		imagejpeg($newImg, dirname($srcImg)."/".$thumbnail."/".basename($srcImg));
		imagedestroy($newImg);
	}
}
?>